using System;
using System.Collections.Generic;
using System.Text;

using NCT.Framework;
using NCT.Mail;

namespace VISS.MailImpl {
    public class MailImpl : IMailEnt {

        public MailMsgEnt BuildMessageEnt(string data) {
            MailEnt.MailInfo mailInfo = SerializerUtils.JsonDeSerialize<MailEnt.MailInfo>(data);
            if(mailInfo.GetType() != typeof(MailEnt.MailInfo)) {
                return null;
            }
            MailMsgEnt item = new MailMsgEnt();
            switch (mailInfo.SendMailType) {
                case ESendMailType.RegisterSuccess:
                    MailEnt.MailRegisterSuccess objRegisterSuccessMail = SerializerUtils.JsonDeSerialize<MailEnt.MailRegisterSuccess>(mailInfo.MailContent);
                    item.ToAddress = objRegisterSuccessMail.Email;
                    item.Body = TemplateBuilder.GetRegisterSuccessTemplate(objRegisterSuccessMail.UserKey, objRegisterSuccessMail.FullName, objRegisterSuccessMail.Email, objRegisterSuccessMail.Password);
                    item.Subject = objRegisterSuccessMail.Subject;
                    break;
                case ESendMailType.Notification:
                    MailEnt.MailNotification objNotificationMail = SerializerUtils.JsonDeSerialize<MailEnt.MailNotification>(mailInfo.MailContent);
                    item.ToAddress = objNotificationMail.Email;
                    item.Body = TemplateBuilder.GetNotificationTemplate(objNotificationMail.UserKey, objNotificationMail.FullName, objNotificationMail.Email, objNotificationMail.Title, objNotificationMail.Content);
                    item.Subject = objNotificationMail.Subject;
                    break;
                case ESendMailType.VerifiedNotification:
                    MailEnt.MailVerifiedNotification objVerifiedNotificationMail = SerializerUtils.JsonDeSerialize<MailEnt.MailVerifiedNotification>(mailInfo.MailContent);
                    item.ToAddress = objVerifiedNotificationMail.Email;
                    item.Body = TemplateBuilder.GetVerifiedNotificationTemplate(objVerifiedNotificationMail.UserKey, objVerifiedNotificationMail.FullName, objVerifiedNotificationMail.Email);
                    item.Subject = objVerifiedNotificationMail.Subject;
                    break;
                case ESendMailType.UnVerifyNotification:
                    MailEnt.MailUnVerifyNotification objUnVerifyNotificationMail = SerializerUtils.JsonDeSerialize<MailEnt.MailUnVerifyNotification>(mailInfo.MailContent);
                    item.ToAddress = objUnVerifyNotificationMail.Email;
                    item.Body = TemplateBuilder.GetUnVerifyNotificationTemplate(objUnVerifyNotificationMail.UserKey, objUnVerifyNotificationMail.FullName, objUnVerifyNotificationMail.Email);
                    item.Subject = objUnVerifyNotificationMail.Subject;
                    break;
                case ESendMailType.MessagesNotification:
                    MailEnt.MailMessagesNotification objMessagesNotificationMail = SerializerUtils.JsonDeSerialize<MailEnt.MailMessagesNotification>(mailInfo.MailContent);
                    item.ToAddress = objMessagesNotificationMail.Email;
                    item.Body = TemplateBuilder.GetMessagesNotificationTemplate(objMessagesNotificationMail.UserKey, objMessagesNotificationMail.FullName, objMessagesNotificationMail.Email, objMessagesNotificationMail.MessagesInDay);
                    item.Subject = objMessagesNotificationMail.Subject;
                    break;
                case ESendMailType.ForgotPassword:
                    MailEnt.ForgotPassword objForgotPasswordMail = SerializerUtils.JsonDeSerialize<MailEnt.ForgotPassword>(mailInfo.MailContent);
                    item.ToAddress = objForgotPasswordMail.Email;
                    item.Body = TemplateBuilder.GetForgotPasswordSuccessTemplate(objForgotPasswordMail.UserKey, objForgotPasswordMail.FullName, objForgotPasswordMail.Email, objForgotPasswordMail.HyperLink);
                    item.Subject = objForgotPasswordMail.Subject;
                    break;
            }
            item.FromAddressDisplayName = ConfigUtils.GetValue("VISS.Mail.FromDisplayName", "viss.vn");
            item.FromAddress = ConfigUtils.GetValue("Admin.Mail.FromAddress", "noreply") + "+" + StringUtils.GetRandomString(8) + ConfigUtils.GetValue("Admin.Mail.Domain", "@viss.vn");
            
            return item;
        }
    }


    /// <summary>
    /// This Entity is used for NCT mail service
    /// </summary>
    [Serializable]
    public class MailEnt {

        /// <summary>
        /// Contains mail content and mail type
        /// </summary>
        [Serializable]
        public class MailInfo {
            /// <summary>
            /// Gets or sets the type of the send mail.
            /// </summary>
            /// <value>The type of the send mail.</value>
            public ESendMailType SendMailType { get; set; }
            /// <summary>
            /// Gets or sets the content of the mail.
            /// </summary>
            /// <value>The content of the mail.</value>
            public string MailContent { get; set; }
        }

        /// <summary>
        /// Register success mail
        /// </summary>
        [Serializable]
        public class MailRegisterSuccess {
            /// <summary>
            /// Gets or sets the user key.
            /// </summary>
            /// <value>The user key.</value>
            public string UserKey { get; set; }
            /// <summary>
            /// Gets or sets the full name.
            /// </summary>
            /// <value>The full name.</value>
            public string FullName { get; set; }
            /// <summary>
            /// Gets or sets the email.
            /// </summary>
            /// <value>The email.</value>
            public string Email { get; set; }
            /// <summary>
            /// Gets or sets the password.
            /// </summary>
            /// <value>The password.</value>
            public string Password { get; set; }
            /// <summary>
            /// Gets or sets the mail subject.
            /// </summary>
            /// <value>The subject.</value>
            public string Subject { get; set; }

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString() {
                return SerializerUtils.JsonSerialize(this);
            }
        }
        /// <summary>
        /// Forgot Password
        /// </summary>
        [Serializable]
        public class ForgotPassword
        {
            /// <summary>
            /// Gets or sets the user key.
            /// </summary>
            /// <value>The user key.</value>
            public string UserKey { get; set; }
            /// <summary>
            /// Gets or sets the full name.
            /// </summary>
            /// <value>The full name.</value>
            public string FullName { get; set; }
            /// <summary>
            /// Gets or sets the email.
            /// </summary>
            /// <value>The email.</value>
            public string Email { get; set; }
            /// <summary>
            /// Gets or sets the password.
            /// </summary>
            /// <value>The password.</value>
            public string HyperLink { get; set; }
            /// <summary>
            /// Gets or sets the mail subject.
            /// </summary>
            /// <value>The subject.</value>
            public string Subject { get; set; }

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString()
            {
                return SerializerUtils.JsonSerialize(this);
            }
        }
        /// <summary>
        /// Notification mail
        /// </summary>
        [Serializable]
        public class MailNotification {
            /// <summary>
            /// Gets or sets the user key.
            /// </summary>
            /// <value>The user key.</value>
            public string UserKey { get; set; }
            /// <summary>
            /// Gets or sets the full name.
            /// </summary>
            /// <value>The full name.</value>
            public string FullName { get; set; }
            /// <summary>
            /// Gets or sets the email.
            /// </summary>
            /// <value>The email.</value>
            public string Email { get; set; }
            /// <summary>
            /// Gets or sets the subject.
            /// </summary>
            /// <value>The subject.</value>
            public string Subject { get; set; }
            /// <summary>
            /// Gets or sets the content.
            /// </summary>
            /// <value>The content.</value>
            public string Content { get; set; }
            /// <summary>
            /// Gets or sets the title.
            /// </summary>
            /// <value>The title.</value>
            public string Title { get; set; }

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString() {
                return SerializerUtils.JsonSerialize(this);
            }
        }

        /// <summary>
        /// Account Verified Notification
        /// </summary>
        [Serializable]
        public class MailVerifiedNotification {
            /// <summary>
            /// Gets or sets the user key.
            /// </summary>
            /// <value>The user key.</value>
            public string UserKey { get; set; }
            /// <summary>
            /// Gets or sets the full name.
            /// </summary>
            /// <value>The full name.</value>
            public string FullName { get; set; }
            /// <summary>
            /// Gets or sets the email.
            /// </summary>
            /// <value>The email.</value>
            public string Email { get; set; }
            /// <summary>
            /// Gets or sets the subject.
            /// </summary>
            /// <value>The subject.</value>
            public string Subject { get; set; }

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString() {
                return SerializerUtils.JsonSerialize(this);
            }
        }

        /// <summary>
        /// Account UnVerified Notification
        /// </summary>
        [Serializable]
        public class MailUnVerifyNotification : MailVerifiedNotification {

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString() {
                return SerializerUtils.JsonSerialize(this);
            }
        }

        /// <summary>
        /// New Mail Messages Notification
        /// </summary>
        [Serializable]
        public class MailMessagesNotification : MailVerifiedNotification {
            /// <summary>
            /// Gets or sets the messages in day.
            /// </summary>
            /// <value>The messages in day.</value>
            public int MessagesInDay { get; set; }

            /// <summary>
            /// Returns a Json <see cref="System.String"/> that represents this instance.
            /// </summary>
            /// <returns>
            /// A <see cref="System.String"/> that represents this instance.
            /// </returns>
            public override string ToString() {
                return SerializerUtils.JsonSerialize(this);
            }
        }

        /// <summary>
        /// Returns a Json <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString() {
            return SerializerUtils.JsonSerialize(this);
        }
    }


    /// <summary>
    /// Values of mail type
    /// </summary>
    public enum ESendMailType {
        /// <summary>
        /// 
        /// </summary>
        RegisterSuccess = 0,
        /// <summary>
        /// 
        /// </summary>
        Notification = 1,
        /// <summary>
        /// 
        /// </summary>
        VerifiedNotification = 2,
        /// <summary>
        /// 
        /// </summary>
        UnVerifyNotification = 3,
        /// <summary>
        /// 
        /// </summary>
        MessagesNotification = 4,
        /// <summary>
        /// 
        /// </summary>
        ForgotPassword = 5
    }
}